﻿using CKS2320.Common.Model;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CKS2320.Controller.Program;
using static System.Net.Mime.MediaTypeNames;

namespace CKS2320.Controller
{
    public partial class Form_Para : Form
    {
        public ParaModel para = new();
        public ParaShowSetModel paraShow = new();

        public Form_Para()
        {
            InitializeComponent();
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;

        }
        //读取补偿参数
        public ParaModel ParaJsonLoad()
        {
            string filePath = "ParaSet.json";
            string text = File.ReadAllText(filePath);
            return JsonConvert.DeserializeObject<ParaModel>(text);
        }
        public bool SavePare(string para)
        {
            string filePath = "ParaSet.json";
            File.WriteAllText(filePath, para);
            return true;
        }
        public ParaShowSetModel ShowSetJsonLoad()
        {
            string filePath = "../api/ParaSet.json";
            //string filePath = "D:\\墨西哥大屏项目\\M3Y 程序\\Xin1AutomaticDetectionEquipment _25S\\CKS2320.WebApi\\bin\\Debug\\net6.0\\ParaSet.json";
            string text = File.ReadAllText(filePath);
            return JsonConvert.DeserializeObject<ParaShowSetModel>(text);
        }
        public bool SaveShowSet(string para)
        {
            string filePath = "../api/ParaSet.json";
           // string filePath = "D:\\墨西哥大屏项目\\M3Y 程序\\Xin1AutomaticDetectionEquipment _25S\\CKS2320.WebApi\\bin\\Debug\\net6.0\\ParaSet.json";
            File.WriteAllText(filePath, para);
            return true;
        }

        private void button_Set_Click(object sender, EventArgs e)
        {

            //para.BDOffset = float.Parse(textBox_BD.Text);
            //para.BSOffset = float.Parse(textBox_BS.Text);
            //para.FDOffset = float.Parse(textBox_FD.Text);
            //para.FSOffset = float.Parse(textBox_FS.Text);
            //para.SC01Offset = float.Parse(textBox_SC01.Text);
            //para.SC02Offset = float.Parse(textBox_SC02.Text);
            //para.SC04Offset = float.Parse(textBox_SC04.Text);
            para.TLStander = float.Parse(textBox_standerTL.Text);
            para.TLTolerance = float.Parse(textBox_toleranceTL.Text);
            para.SC01Stander = float.Parse(textBox_standerSC01.Text);
            para.SC02Stander = float.Parse(textBox_standerSC02.Text);
            para.SC03Stander = float.Parse(textBox_standerSC03.Text);
            para.SC04Stander = float.Parse(textBox_standerSC04.Text);
            para.SC01Tolerance = float.Parse(textBox_toleranceSC01.Text);
            para.SC02Tolerance = float.Parse(textBox_toleranceSC02.Text);
            para.SC03Tolerance = float.Parse(textBox_toleranceSC03.Text);
            para.SC04Tolerance = float.Parse(textBox_toleranceSC04.Text);
            para.SC04Tolerance = float.Parse(textBox_toleranceSC04.Text);
            paraShow.FS1 = textBox_fs1.Text;
            paraShow.FS2 = textBox_fs2.Text;
            paraShow.FD1 = textBox_fd1.Text;
            paraShow.FD2 = textBox_fd2.Text;
            paraShow.BS1 = textBox_bs1.Text;
            paraShow.BS2 = textBox_bs2.Text;
            paraShow.BD1 = textBox_bd1.Text;
            paraShow.BD2 = textBox_bd2.Text;
            paraShow.TKA = textBox_TKA.Text;
            paraShow.TKB = textBox_TKB.Text;
            string jsonpara = JsonConvert.SerializeObject(para);
            string jsonshow = JsonConvert.SerializeObject(paraShow);
            if (SavePare(jsonpara) && SaveShowSet(jsonshow))
            {
                MessageBox.Show("参数设置成功");
            }


        }

        private void Form_Para_Load(object sender, EventArgs e)
        {
            para = ParaJsonLoad();
            paraShow = ShowSetJsonLoad();
            //textBox_BD.Text = para.BDOffset.ToString();
            //textBox_FD.Text = para.FDOffset.ToString();
            //textBox_BS.Text = para.BSOffset.ToString();
            //textBox_FS.Text = para.FSOffset.ToString();
            //textBox_SC01.Text = para.SC01Offset.ToString();
            //textBox_SC02.Text = para.SC02Offset.ToString();
            //textBox_SC04.Text = para.SC04Offset.ToString();
            textBox_standerTL.Text = para.TLStander.ToString();
            textBox_standerSC01.Text = para.SC01Stander.ToString();
            textBox_standerSC02.Text = para.SC02Stander.ToString();
            textBox_standerSC03.Text = para.SC03Stander.ToString();
            textBox_standerSC04.Text = para.SC04Stander.ToString();
            textBox_toleranceTL.Text = para.TLTolerance.ToString();
            textBox_toleranceSC01.Text = para.SC01Tolerance.ToString();
            textBox_toleranceSC02.Text = para.SC02Tolerance.ToString();
            textBox_toleranceSC03.Text = para.SC03Tolerance.ToString();
            textBox_toleranceSC04.Text = para.SC04Tolerance.ToString();
            textBox_fs1.Text = paraShow.FS1;
            textBox_fs2.Text = paraShow.FS2;
            textBox_fd1.Text = paraShow.FD1;
            textBox_fd2.Text = paraShow.FD2;
            textBox_bs1.Text = paraShow.BS1;
            textBox_bs2.Text = paraShow.BS2;
            textBox_bd1.Text = paraShow.BD1;
            textBox_bd2.Text = paraShow.BD2;
            textBox_TKA.Text = paraShow.TKA;
            textBox_TKB.Text = paraShow.TKB;
        }


        private void textBox_standerSC01_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_standerSC01.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }
        }
        private void textBox_standerSC02_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_standerSC02.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }
        }
        private void textBox_standerSC03_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_standerSC03.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }
        }
        private void textBox_standerSC04_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_standerSC04.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox_toleranceSC01_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_toleranceSC01.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox_toleranceSC02_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_toleranceSC02.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBox_toleranceSC03_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_toleranceSC03.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }

        }

        private void textBox_toleranceSC04_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '-' || e.KeyChar == 8)
            {
                if (textBox_toleranceSC04.Text.Contains('.') && e.KeyChar == '.')
                { e.Handled = true; }
                else { e.Handled = false; }
            }
            else
            {
                e.Handled = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer", "图纸");
        }
    }

}

